package org.scboy.dbms.loads;

import org.scboy.coobi.sql.Coobi;
import org.scboy.coobi.sql.factory.EntityFactory;
import org.scboy.dbms.dao.BaseSystemConfigDao;
import org.scboy.utils.caches.DataCache;
import org.scboy.utils.caches.dbms.entity.SystemConfigEntity;
import org.scboy.utils.entitys.coobi.Model;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Configuration;

import cn.hutool.log.Log;
import cn.hutool.log.LogFactory;

/**
 * 系统配置缓存管理
 */
@Configuration
@EnableCaching
@AutoConfigureAfter(value = {Coobi.class,EntityFactory.class})
public class LoadSystemConfig implements DataCache<SystemConfigEntity> {

	@Autowired
	EntityFactory factory;

	Log log =LogFactory.get();
	
	@Override
	@Cacheable(value="DBMS_SYSCONFIG_CACHE",key="#code",unless="#result == null")
	public SystemConfigEntity get(String code) {
		BaseSystemConfigDao dao=factory.getDao(BaseSystemConfigDao.class);
		Model config=dao.get(Model.create().add("code", code));
		SystemConfigEntity entity=new SystemConfigEntity();
		entity.setCode(config.getString("code"));
		entity.setValue(config.getString("configValue"));
		return entity;
	}

	@Override
	@CachePut(value="DBMS_SYSCONFIG_CACHE",key = "#result.code",unless="#result == null")
	public SystemConfigEntity update(SystemConfigEntity data) {
		return data;
	}

	@Override
	@CachePut(value="DBMS_SYSCONFIG_CACHE",key = "#result.code",unless="#result == null")
	public SystemConfigEntity updateByKey(String code) {
		return get(code);
	}

	@Override
	@CacheEvict(value="DBMS_SYSCONFIG_CACHE",key = "#code")
	public SystemConfigEntity removeCache(String code) {
		return null;
	}

	@Override
	@CacheEvict(value="DBMS_SYSCONFIG_CACHE",allEntries = true)
	public void removeAll() {}

}
